<?php
class patientController extends AController {
	
	// 客户列表
	public function indexAction() {
		$keyword = $this->request->getGetParameter('keyword');
		$page = $this->request->getGetParameter('p');
		$pageSize = 10;
		if (!$page) $page = 1;
		
		$where = $keyword ? "TRUE_NAME LIKE '%{$keyword}%' OR CARD LIKE '%{$keyword}%' OR PHONE LIKE '%{$keyword}%'" : '';
		$fields = "PATIENT_ID, TRUE_NAME, SEX, PHONE, CARD, BIRTH, SOCIAL_CARD, LXR_NAME, LXR_PHONE, RELATION";

		$totalCount = 0;
		$this->rows = JYDB::findAll('PATIENT', $where, $fields, '', $page, 10, $totalCount);
		$this->pager = JYTools::pager('index.php?c=patient&a=index&p=_num_', $totalCount, $page);
		return $this->render();
	}
	
	// 客户修改
	public function formAction() {
		$pid = $this->request->getGetParameter('pid');
		if ($pid) {
			$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$pid}");
		}
		return $this->render();
	}
	
	// 客户导入
	public function importformAction() {
		return $this->render();
	}
	
	// 客户信息查看页
	public function entryAction() {
		$pid = $this->request->getGetParameter('pid', 0);
		$phone = $this->request->getGetParameter('phone');
		/* $phone = '13530877460'; */
		// 获取病人基本资料
		$patient = JYDB::find('PATIENT', "PATIENT_ID={$pid} or PHONE='{$phone}'");
		if ($patient) {
			$zhuyuan = JYDB::find('ZHUYUAN', "REF_PATIENT_ID={$patient['PATIENT_ID']}", '*', 'CY_DATE DESC');
			$this->pid = $patient['PATIENT_ID'];
			$this->patient = $patient;
			$this->zhuyuan = $zhuyuan;
			return $this->render();
		} else { 
			// 进入未注册页面
			$this->phone = $phone;
			$this->alldep = JYDep::getMzDepts(CSM_UNIT_ID);
			return $this->render('patient/unknown.php');
		}
	}
	
	// 客户资料修改对话框
	public function formdlgAction() {
		$pid = $this->request->getGetParameter('pid');
		if ($pid) {
			$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$pid}");
		}
		
		return $this->render();
	}
	
	// 客户资料修改保存
	public function saveAction() {
		if(ATools::isAjax()){
			return $this->saveByJson();
		} else {
			return '';
		}
	}
	
	private function saveByJson() {
		$row['TRUE_NAME'] = $this->request->getPostParameter('TRUE_NAME');
		$row['SEX'] = $this->request->getPostParameter('SEX');
		$row['PHONE'] = $this->request->getPostParameter('PHONE');
		$row['CARD'] = $this->request->getPostParameter('CARD');
		$row['SOCIAL_CARD'] = $this->request->getPostParameter('SOCIAL_CARD');
		$row['LXR_NAME'] = $this->request->getPostParameter('LXR_NAME');
		$row['RELATION'] = $this->request->getPostParameter('RELATION');
		$row['LXR_PHONE'] = $this->request->getPostParameter('LXR_PHONE');
		$pid = $this->request->getPostParameter('PID');
		
		if ($pid) {
			if (CSM_UNIT_ID > 0) {
				$rs = JYDB::update('PATIENT', "PATIENT_ID={$pid} AND UNIT_ID=".CSM_UNIT_ID, $row);
			} else {
				$rs = JYDB::update('PATIENT', "PATIENT_ID={$pid}", $row);
			}
		} else {
			$row['UNIT_ID'] = CSM_UNIT_ID;
			$rs = JYDB::create('PATIENT', $row, 'PATIENT_ID');
		}
		if ($rs) {
			return json_encode(array('code'=>1, 'msg'=>'客户资料保存成功'));
		} else {
			return json_encode(array('code'=>-1, 'msg'=>'客户资料保存失败'));
		}
	}
	
	public function registAction() {
		if(ATools::isAjax()){
			return $this->registByJson();
		} else {
			return '';
		}
	}
	
	private function registByJson() {
		$row['TRUE_NAME'] = $this->request->getPostParameter('name', '匿名用户');
		$row['PHONE'] = $this->request->getPostParameter('phone');
		$row['SEX'] = $this->request->getPostParameter('sex');
		$row['CARD'] = $this->request->getPostParameter('card');
		$row['JOB'] = $this->request->getPostParameter('job');
		$row['ADDR'] = $this->request->getPostParameter('address');
		$row['UNIT_ID'] = CSM_UNIT_ID;
		$row['CREATE_TIME'] = date('Y-m-d H:i:s');
		$row['CREATE_SYS_ID'] = $this->request->getGetParameter('sysid');
		
		// 判断电话号码是否存在，如果存在，直接返回当前用户的用户资料即可
		$rs = JYDB::find("PATIENT", "PHONE='{$row['PHONE']}'", "PATIENT_ID");
		if ($rs) {
			return json_encode(array('code' => 2, 'msg'=>'注册成功', 'data'=>array('id'=>$rs['PATIENT_ID'])));
		} else {
			$rs = JYDB::create('PATIENT', $row, 'PATIENT_ID');
			if ($rs) {
				return json_encode(array('code' => 1, 'msg'=>'注册成功', 'data'=>array('id'=>$rs)));
			} else {
				return json_encode(array('code'=> -1, 'msg'=>'注册失败'));
			}
		}
	}
	
}